﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Security.AccessControl;
using System.Text;
using System.Threading.Tasks;

namespace QuotingTool.Utility
{
    public class Common
    {
        public static DataTable ToDataTable<T>(IList<T> data)
        {
            //Get properties
            PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
            //.Where(p => !p.GetGetMethod().IsVirtual && !p.GetGetMethod().IsFinal).ToArray(); //Hides virtual properties

            //Get column headers
            bool isDisplayNameAttributeDefined = false;
            string[] headers = new string[Props.Length];
            int colCount = 0;
            foreach (PropertyInfo prop in Props)
            {
                isDisplayNameAttributeDefined = Attribute.IsDefined(prop, typeof(DisplayNameAttribute));

                if (isDisplayNameAttributeDefined)
                {
                    DisplayNameAttribute dna = (DisplayNameAttribute)Attribute.GetCustomAttribute(prop, typeof(DisplayNameAttribute));
                    if (dna != null)
                        headers[colCount] = dna.DisplayName;
                }
                else
                    headers[colCount] = prop.Name;

                colCount++;
                isDisplayNameAttributeDefined = false;
            }

            DataTable dataTable = new DataTable(typeof(T).Name);

            //Add column headers to datatable
            foreach (var header in headers)
                dataTable.Columns.Add(header);

            //Add datalist to datatable
            foreach (T item in data)
            {
                object[] values = new object[Props.Length];
                for (int col = 0; col < Props.Length; col++)
                    values[col] = Props[col].GetValue(item, null);

                dataTable.Rows.Add(values);
            }
            foreach (var column in dataTable.Columns.Cast<DataColumn>().ToArray())
            {
                if (dataTable.AsEnumerable().All(dr => dr.IsNull(column)))
                    dataTable.Columns.Remove(column);
            }
            dataTable.AcceptChanges();
            return dataTable;
        }


        ///// <summary>
        ///// To convert a List to datatable
        ///// </summary>
        ///// <typeparam name="T">Type of list</typeparam>
        ///// <param name="iList">collection of list</param>
        ///// <returns></returns>
        //public static DataTable ToDataTable<T>(List<T> items)
        //{
        //    DataTable dataTable = new DataTable(typeof(T).Name);

        //    //Get all the properties
        //    PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
        //    foreach (PropertyInfo prop in Props)
        //    {
        //        //Setting column names as Property names
        //        dataTable.Columns.Add(prop.Name);
        //    }
        //    foreach (T item in items)
        //    {
        //        var values = new object[Props.Length];
        //        for (int i = 0; i < Props.Length; i++)
        //        {
        //            //inserting property values to datatable rows
        //            values[i] = Props[i].GetValue(item, null);
        //        }
        //        dataTable.Rows.Add(values);
        //    }
        //    //put a breakpoint here and check datatable
        //    return dataTable;
        //}

        
    }
}
